package com.ruoyi.pump.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.dc3.api.center.manager.feign.PointClient;
import com.ruoyi.dc3.api.center.manager.feign.PointInfoClient;
import com.ruoyi.dc3.common.model.Point;
import com.ruoyi.dc3.common.model.PointInfoDP;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pump.domain.Dc3Point;
import com.ruoyi.pump.service.IDc3PointService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * dc3节点信息Controller
 * 
 * @author ruoyi
 * @date 2025-09-18
 */
@RestController
@RequestMapping("/pump/dc3point")
public class Dc3PointController extends BaseController
{
    @Autowired
    private IDc3PointService dc3PointService;

    /**
     * 查询dc3节点信息列表
     */
    @PreAuthorize("@ss.hasPermi('pump:dc3point:list')")
    @GetMapping("/list")
    public TableDataInfo list(Dc3Point dc3Point)
    {
        startPage();
        List<Dc3Point> list = dc3PointService.selectDc3PointList(dc3Point);
        return getDataTable(list);
    }

    /**
     * 导出dc3节点信息列表
     */
    @PreAuthorize("@ss.hasPermi('pump:dc3point:export')")
    @Log(title = "dc3节点信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Dc3Point dc3Point)
    {
        List<Dc3Point> list = dc3PointService.selectDc3PointList(dc3Point);
        ExcelUtil<Dc3Point> util = new ExcelUtil<Dc3Point>(Dc3Point.class);
        util.exportExcel(response, list, "dc3节点信息数据");
    }

    /**
     * 获取dc3节点信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('pump:dc3point:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dc3PointService.selectDc3PointById(id));
    }

    /**
     * 新增dc3节点信息
     */
    @PreAuthorize("@ss.hasPermi('pump:dc3point:add')")
    @Log(title = "dc3节点信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Dc3Point dc3Point)
    {
        return toAjax(dc3PointService.insertDc3Point(dc3Point));
    }

    /**
     * 修改dc3节点信息
     */
    @PreAuthorize("@ss.hasPermi('pump:dc3point:edit')")
    @Log(title = "dc3节点信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Dc3Point dc3Point)
    {
        return toAjax(dc3PointService.updateDc3Point(dc3Point));
    }

    /**
     * 删除dc3节点信息
     */
    @PreAuthorize("@ss.hasPermi('pump:dc3point:remove')")
    @Log(title = "dc3节点信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dc3PointService.deleteDc3PointByIds(ids));
    }


    @Autowired
    PointClient pointClient;
    @Autowired
    PointInfoClient pointInfoClient;

    @PostMapping("/sync")
    public AjaxResult sync() {
        List<PointInfoDP> pointInfoDPs = pointInfoClient.listdevicepoint().getData();
        pointInfoDPs.forEach(pointInfoDP -> {
            Dc3Point dcPoint = new Dc3Point();
            dcPoint.setId(pointInfoDP.getPointId());
            dcPoint.setDid(pointInfoDP.getDeviceId());
            dcPoint.setName(pointInfoDP.getPointName());

            Point point = pointClient.selectById(pointInfoDP.getPointId()).getData();

            dcPoint.setUnit(point.getUnit());
            dcPoint.setRemark(point.getDescription());

            if (getInfo(pointInfoDP.getPointId()).containsKey("data")) {
                edit(dcPoint);
            }else{
                add(dcPoint);
            }
        });
        return AjaxResult.success("同步成功");
    }
}
